import Pbf from './Pdf'

declare type ReadAndWrite =
{
    read: (t: Pbf, e: number) => any,
    IQ: (t: number, e: any, r: Pbf) => void,
    write: (t: any, e: Pbf) => void 
}

declare type IWReadAndWrite =
{
    read: (t: Pbf, e: number) => any,
    iw: (t: number, e: any, r: Pbf) => void,
    write: (t: any, e: Pbf) => void 
}

var Ring: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(Ring.iw, {
          path: []
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t && r.tw(e.path, !0)
  },
  write(t: any, e: Pbf) {
      if (t.path)
          for (var r = 0; r < t.path.length; r++)
              e.rw(0, t.path[r])
  }
}
, Building: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(Building.iw, {
          minzoom: 0,
          maxzoom: 0,
          path: [],
          height: 0,
          ew: "",
          altitude: 0
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t ? e.minzoom = r.ow() : 
        1 === t ? e.maxzoom = r.ow() : 
            2 === t ? e.path.push(Ring.read(r, r.ow() + r.pos)) : 
                3 === t ? e.height = r.ow() : 
                    4 === t ? e.ew = r.aw() : 
                        5 === t && (e.altitude = r.ow())
  },
  write(t: any, e: Pbf) {
      if (t.minzoom && e.rw(0, t.minzoom),
      t.maxzoom && e.rw(1, t.maxzoom),
      t.path)
          for (var r = 0; r < t.path.length; r++)
              e.fw(2, Ring.write, t.path[r]);
      t.height && e.rw(3, t.height),
      t.ew && e.uw(4, t.ew),
      t.altitude && e.rw(5, t.altitude)
  }
}
, BuildingSameStyle: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(BuildingSameStyle.iw, {
          mainkey: 0,
          subkey: 0,
          items: [],
          resolution: 0
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t ? e.mainkey = r.ow() : 1 === t ? e.subkey = r.ow() : 2 === t ? e.items.push(Building.read(r, r.ow() + r.pos)) : 3 === t && (e.resolution = r.ow())
  },
  write(t: any, e: Pbf) {
      if (t.mainkey && e.rw(0, t.mainkey),
      t.subkey && e.rw(1, t.subkey),
      t.items)
          for (var r = 0; r < t.items.length; r++)
              e.fw(2, Building.write, t.items[r]);
      t.resolution && e.rw(3, t.resolution)
  }
}
, buildingLayer: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(buildingLayer.iw, {
          building: []
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t && e.building.push(BuildingSameStyle.read(r, r.ow() + r.pos))
  },
  write(t: any, e: Pbf) {
      if (t.building)
          for (var r = 0; r < t.building.length; r++)
              e.fw(0, BuildingSameStyle.write, t.building[r])
  }
}
, Poi: ReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(Poi.IQ, {
          minzoom: 0,
          maxzoom: 0,
          pos: [],
          cw: "",
          ew: "",
          name: "",
          rank: 0,
          BQ: "",
          name_en: "",
          QQ: "",
          CQ: "",
          $G: [],
          iQ: [],
          JQ: []
      }, e)
  },
  IQ(t: number, e: any, r: Pbf) {
      0 === t ? e.minzoom = r.ow() : 1 === t ? e.maxzoom = r.ow() : 2 === t ? r.tw(e.pos, !0) : 3 === t ? e.cw = r.aw() : 4 === t ? e.ew = r.aw() : 5 === t ? e.name = r.aw() : 6 === t ? e.rank = r.ow() : 7 === t ? e.BQ = r.aw() : 8 === t ? e.name_en = r.aw() : 9 === t ? e.QQ = r.aw() : 10 === t ? e.CQ = r.aw() : 11 === t ? r.tw(e.$G, false) : 12 === t ? r.tw(e.iQ, false) : 13 === t && r.tw(e.JQ, false)
  },
  write(t: any, e: Pbf) {
      if (t.minzoom && e.rw(0, t.minzoom),
      t.maxzoom && e.rw(1, t.maxzoom),
      t.pos)
          for (var r = 0; r < t.pos.length; r++)
              e.rw(2, t.pos[r]);
      if (t.cw && e.uw(3, t.cw),
      t.ew && e.uw(4, t.ew),
      t.name && e.uw(5, t.name),
      t.rank && e.rw(6, t.rank),
      t.BQ && e.uw(7, t.BQ),
      t.name_en && e.uw(8, t.name_en),
      t.QQ && e.uw(9, t.QQ),
      t.CQ && e.uw(10, t.CQ),
      t.$G)
          for (r = 0; r < t.$G.length; r++)
              e.rw(11, t.$G[r]);
      if (t.iQ)
          for (r = 0; r < t.iQ.length; r++)
              e.rw(12, t.iQ[r]);
      if (t.JQ)
          for (r = 0; r < t.JQ.length; r++)
              e.rw(13, t.JQ[r])
  }
}
, PoiSameStyle: ReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(PoiSameStyle.IQ, {
          mainkey: 0,
          subkey: 0,
          items: [],
          resolution: 0
      }, e)
  },
  IQ(t: number, e: any, r: Pbf) {
      0 === t ? e.mainkey = r.ow() : 1 === t ? e.subkey = r.ow() : 2 === t ? e.items.push(Poi.read(r, r.ow() + r.pos)) : 3 === t && (e.resolution = r.ow())
  },
  write(t: any, e: Pbf) {
      if (t.mainkey && e.rw(0, t.mainkey),
      t.subkey && e.rw(1, t.subkey),
      t.items)
          for (var r = 0; r < t.items.length; r++)
              e.fw(2, Poi.write, t.items[r]);
      t.resolution && e.rw(3, t.resolution)
  }
}
, PoiLayer: ReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(PoiLayer.IQ, {
          poilabel: []
      }, e)
  },
  IQ(t: number, e: any, r: Pbf) {
      0 === t && e.poilabel.push(PoiSameStyle.read(r, r.ow() + r.pos))
  },
  write(t: any, e: Pbf) {
      if (t.poilabel)
          for (var r = 0; r < t.poilabel.length; r++)
              e.fw(0, PoiSameStyle.write, t.poilabel[r])
  }
}
, Ring$1: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(Ring$1.iw, {
          path: []
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t && r.tw(e.path, !0)
  },
  write(t: any, e: Pbf) {
      if (t.path)
          for (var r = 0; r < t.path.length; r++)
              e.rw(0, t.path[r])
  }
}
, Region: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(Region.iw, {
          minzoom: 0,
          maxzoom: 0,
          path: [],
          sw: ""
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t ? e.minzoom = r.ow() : 1 === t ? e.maxzoom = r.ow() : 2 === t ? e.path.push(Ring$1.read(r, r.ow() + r.pos)) : 3 === t && (e.sw = r.aw())
  },
  write(t: any, e: Pbf) {
      if (t.minzoom && e.rw(0, t.minzoom),
      t.maxzoom && e.rw(1, t.maxzoom),
      t.path)
          for (var r = 0; r < t.path.length; r++)
              e.fw(2, Ring$1.write, t.path[r]);
      t.sw && e.uw(3, t.sw)
  }
}
, RegionSameStyle: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(RegionSameStyle.iw, {
          mainkey: 0,
          subkey: 0,
          items: [],
          resolution: 0
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t ? e.mainkey = r.ow() : 1 === t ? e.subkey = r.ow() : 2 === t ? e.items.push(Region.read(r, r.ow() + r.pos)) : 3 === t && (e.resolution = r.ow())
  },
  write(t: any, e: Pbf) {
      if (t.mainkey && e.rw(0, t.mainkey),
      t.subkey && e.rw(1, t.subkey),
      t.items)
          for (var r = 0; r < t.items.length; r++)
              e.fw(2, Region.write, t.items[r]);
      t.resolution && e.rw(3, t.resolution)
  }
}
, regionLayer: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(regionLayer.iw, {
          region: []
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t && e.region.push(RegionSameStyle.read(r, r.ow() + r.pos))
  },
  write(t: any, e: Pbf) {
      if (t.region)
          for (var r = 0; r < t.region.length; r++)
              e.fw(0, RegionSameStyle.write, t.region[r])
  }
}
, Road: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(Road.iw, {
          minzoom: 0,
          maxzoom: 0,
          path: [],
          shield: "",
          shieldType: 0,
          name: "",
          rank: 0,
          name_en: "",
          BQ: "",
          QQ: "",
          CQ: "",
          $G: [],
          iQ: []
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t ? e.minzoom = r.ow() : 1 === t ? e.maxzoom = r.ow() : 2 === t ? r.tw(e.path, !0) : 3 === t ? e.shield = r.aw() : 4 === t ? e.shieldType = r.ow() : 5 === t ? e.name = r.aw() : 6 === t ? e.rank = r.ow() : 7 === t ? e.BQ = r.aw() : 8 === t ? e.name_en = r.aw() : 9 === t ? e.QQ = r.aw() : 10 === t ? e.CQ = r.aw() : 11 === t ? r.tw(e.$G, false) : 12 === t && (e.id = r.ow())
  },
  write(t: any, e: Pbf) {
      if (t.minzoom && e.rw(0, t.minzoom),
      t.maxzoom && e.rw(1, t.maxzoom),
      t.path)
          for (var r = 0; r < t.path.length; r++)
              e.rw(2, t.path[r]);
      if (t.shield && e.uw(3, t.shield),
      t.shieldType && e.rw(4, t.shieldType),
      t.name && e.uw(5, t.name),
      t.rank && e.rw(6, t.rank),
      t.BQ && e.uw(7, t.BQ),
      t.name_en && e.uw(8, t.name_en),
      t.QQ && e.uw(9, t.QQ),
      t.CQ && e.uw(10, t.CQ),
      t.$G)
          for (r = 0; r < t.$G.length; r++)
              e.rw(11, t.$G[r]);
      t.id && e.rw(12, t.id)
  }
}
, RoadSameStyle: IWReadAndWrite = {
  read(t: Pbf, e: number) {
      return t.nw(RoadSameStyle.iw, {
          mainkey: 0,
          subkey: 0,
          items: [],
          resolution: 0
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t ? e.mainkey = r.ow() : 1 === t ? e.subkey = r.ow() : 2 === t ? e.items.push(Road.read(r, r.ow() + r.pos)) : 3 === t && (e.resolution = r.ow())
  },
  write(t: any, e: Pbf) {
      if (t.mainkey && e.rw(0, t.mainkey),
      t.subkey && e.rw(1, t.subkey),
      t.items)
          for (var r = 0; r < t.items.length; r++)
              e.fw(2, Road.write, t.items[r]);
      t.resolution && e.rw(3, t.resolution)
  }
}
, roadLayer = {
  read(t: Pbf, e: number) {
      return t.nw(roadLayer.iw, {
          road: []
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t && e.road.push(RoadSameStyle.read(r, r.ow() + r.pos))
  },
  write(t: any, e: Pbf) {
      if (t.road)
          for (var r = 0; r < t.road.length; r++)
              e.fw(0, RoadSameStyle.write, t.road[r])
  }
}
, roadNameLayer = {
  read(t: Pbf, e: number = 0) {
      return t.nw(roadNameLayer.iw, {
          roadName: []
      }, e)
  },
  iw(t: number, e: any, r: Pbf) {
      0 === t && e.roadName.push(RoadSameStyle.read(r, r.ow() + r.pos))
  },
  write(t: any, e: Pbf) {
      if (t.roadName)
          for (var r = 0; r < t.road.length; r++)
              e.fw(0, RoadSameStyle.write, t.roadName[r])
  }
}
, Result = {
  read(t: Pbf, e: number) {
      return t.nw(Result.IQ, {
          tiles: [],
          version: "",
          status: !1,
          db: ""
      }, e)
  },
  IQ(t: number, e: any, r: Pbf) {
      0 === t ? e.tiles.push(Result.Gh.read(r, r.ow() + r.pos)) : 1 === t ? e.version = r.aw() : 2 === t ? e.status = r.vw() : 3 === t && (e.db = r.aw())
  },
  write(t: any, e: Pbf) {
      if (t.tiles)
          for (var r = 0; r < t.tiles.length; r++)
              e.fw(0, Result.Gh.write, t.tiles[r]);
      t.version && e.uw(1, t.version),
      t.status && e.hw(2, t.status),
      t.db && e.uw(3, t.db)
  },
  Gh: {
    read: (t: Pbf, e: number): any => {},
    IQ: (t: number, e: any, r: Pbf): void => {},
    write: (t: any, e: Pbf): void => {},
    lw: {
        read: (t: Pbf, e: number): any => {},
        IQ: (t: number, e: any, r: Pbf): void => {},
        write: (t: any, e: Pbf): void => {} 
    }
  }
};
Result.Gh.read = function(t: Pbf, e: number) {
  return t.nw(Result.Gh.IQ, {
      z: 0,
      x: 0,
      y: 0,
      layers: [],
      t: 0
  }, e)
}
,
Result.Gh.IQ = function(t: number, e: any, r: Pbf) {
  0 === t ? e.z = r.ow() : 1 === t ? e.x = r.ow() : 2 === t ? e.y = r.ow() : 3 === t ? e.layers.push(Result.Gh.lw.read(r, r.ow() + r.pos)) : 4 === t && (e.t = r.ow())
}
,
Result.Gh.write = function(t: any, e: Pbf) {
  if (t.z && e.rw(0, t.z),
  t.x && e.rw(1, t.x),
  t.y && e.rw(2, t.y),
  t.layers)
      for (var r = 0; r < t.layers.length; r++)
          e.fw(3, Result.Gh.lw.write, t.layers[r]);
  t.t && e.rw(4, t.t)
}
,
Result.Gh.lw.read = function(t: Pbf, e: number) {
  return t.nw(Result.Gh.lw.IQ, {
      z: 0,
      x: 0,
      y: 0,
      type: 0,
      d: null,
      t: 0
  }, e)
}
,
Result.Gh.lw.IQ = function(t: number, e: any, r: Pbf) {
  0 === t ? e.z = r.ow() : 1 === t ? e.x = r.ow() : 2 === t ? e.y = r.ow() : 3 === t ? e.type = r.ow() : 4 === t ? e.d = r.mw() : 5 === t && (e.t = r.ow())
}
,
Result.Gh.lw.write = function(t, e) {
  t.z && e.rw(0, t.z),
  t.x && e.rw(1, t.x),
  t.y && e.rw(2, t.y),
  t.type && e.rw(3, t.type),
  t.d && e.ww(4, t.d),
  t.t && e.rw(5, t.t)
}
;
var Style = {
  read(t: Pbf, e: number) {
      return t.nw(Style.IQ, {
          bgColor: "",
          zn: 0,
          xV: "",
          stylemaps: [],
          test: 0
      }, e)
  },
  IQ(t: number, e: any, r: Pbf) {
      1 === t ? e.bgColor = r.aw() : 2 === t ? e.zn = r.ow(!0) : 3 === t ? e.xV = r.aw() : 4 === t ? e.stylemaps.push(Style.SV.read(r, r.ow() + r.pos)) : 5 === t && (e.test = r.ow(!0))
  },
  write(t: any, e: Pbf) {
      if (t.bgColor && e.uw(1, t.bgColor),
      t.zn && e.rw(2, t.zn),
      t.xV && e.uw(3, t.xV),
      t.stylemaps)
          for (var r = 0; r < t.stylemaps.length; r++)
              e.fw(4, Style.SV.write, t.stylemaps[r]);
      t.test && e.rw(5, t.test)
  },
  SV: {
    read: (t: Pbf, e: number): any => {},
    IQ: (t: number, e: any, r: Pbf): void => {},
    write: (t: any, e: Pbf): void => {} 
  }
};
Style.SV.read = function(t: Pbf, e: number) {
  return t.nw(Style.SV.IQ, {
      key: [],
      browserStyle: [],
      levelStyleMapping: []
  }, e)
}
,
Style.SV.IQ = function(t: number, e: any, r: Pbf) {
  1 === t ? e.key.push(r.aw()) : 2 === t ? e.browserStyle.push(r.aw()) : 3 === t && r.tw(e.levelStyleMapping, !0)
}
,
Style.SV.write = function(t, e) {
  if (t.key)
      for (var r = 0; r < t.key.length; r++)
          e.uw(1, t.key[r]);
  if (t.browserStyle)
      for (r = 0; r < t.browserStyle.length; r++)
          e.uw(2, t.browserStyle[r]);
  if (t.levelStyleMapping)
      for (r = 0; r < t.levelStyleMapping.length; r++)
          e.rw(3, t.levelStyleMapping[r])
}
;

export const decodeStyle = function(t: Uint8Array) {
  return Style.read(new Pbf(t), 0)
};
var decodePois = function(t: Uint8Array) {
  return PoiLayer.read(new Pbf(t), 0)
}, decodeRoads = function(t: Uint8Array) {
    return roadLayer.read(new Pbf(t), 0)
}, decodeRoadNames = function(t: Uint8Array) {
    return roadNameLayer.read(new Pbf(t), 0)
}, decodeRegions = function(t: Uint8Array) {
    return regionLayer.read(new Pbf(t), 0)
}, decodeBuildings = function(t: Uint8Array) {
    return buildingLayer.read(new Pbf(t), 0)
}, decodeTiles = function(z: Uint8Array) {
  for (var t = new Pbf(z), e = (t = Result.read(new Pbf(z), 0)).tiles, r = 0, n = e.length; r < n; r += 1)
      for (var i = e[r].layers, a = 0, o = i.length; a < o; a += 1)
          switch (i[a].type) {
          case 0:
              i[a].d = decodePois(i[a].d);
              break;
          case 1:
              i[a].d = decodeRoads(i[a].d);
              break;
          case 2:
              i[a].d = decodeRegions(i[a].d);
              break;
          case 3:
              i[a].d = decodeBuildings(i[a].d);
              break;
          case 4:
              i[a].d = decodeRoadNames(i[a].d)
          }
  return t
}

export default decodeTiles